//
//  UITextFiled+Extension.swift
//  Test
//
//  Created by ZWJY on 2021/8/20.
//

import Foundation
import UIKit

// MARK: - 快速创建UITextField
public extension UITextField{
    
    convenience init(text: String?, textColor: UIColor, font: UIFont, placeholder: String?, placeholderColor: UIColor?, placeholderFont: UIFont? = nil) {
        self.init()
        self.text = text
        self.textColor = textColor
        self.font = font
        self.attributedPlaceholder = NSMutableAttributedString.init(string: placeholder ?? "",
                                                                    attributes: [
                                                                        .font: placeholderFont ?? font,
                                                                        .foregroundColor: placeholderColor ?? UIColor.gray
                                                                    ])
    }
    
    func pg_text(_ text: String?) -> Self {
        self.text = text
        return self
    }
    
    func pg_font(_ font: UIFont?) -> Self {
        self.font = font
        return self
    }
    
    func pg_textColor(_ color: UIColor?) -> Self {
        self.textColor = color
        return self
    }
    
    func pg_tintColor(_ color: UIColor?) -> Self {
        self.tintColor = color
        return self
    }
    
    func pg_placeholder(_ placeholder: String?) -> Self {
        self.placeholder = placeholder
        return self
    }
    
    func pg_returnKeyType(_ returnKeyType: UIReturnKeyType) -> Self {
        self.returnKeyType = returnKeyType
        return self
    }
    
    func pg_keyboardType(_ keyboardType: UIKeyboardType) -> Self {
        self.keyboardType = keyboardType
        return self
    }
    
    func pg_clearButtonMode(_ mode: ViewMode) -> Self {
        self.clearButtonMode = mode
        return self
    }
    
    func pg_delegate(_ delegate: UITextFieldDelegate?) -> Self {
        self.delegate = delegate
        return self
    }
}

public extension UITextField {
    
    /// 添加左边间距
    /// - Parameter padding: 间距
    /// - Returns: tf
    func addLeftPadding(padding: CGFloat) -> Self{
        let leftView = UIView.init(frame: CGRect(x: 0, y: 0, width: padding, height: 1))
        return addLeftView(leftView: leftView)
    }
    
    /// 添加左边View
    /// - Parameter leftView: view
    /// - Returns: tf
    func addLeftView(leftView: UIView?) -> Self {
        self.leftView = leftView
        self.leftViewMode = .always
        return self
    }
    
    /// 添加右边View
    /// - Parameter rightView: view
    /// - Returns: tf
    func addRightView(rightView: UIView?) -> Self {
        self.rightView = rightView
        self.rightViewMode = .always
        return self
    }
}
